﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using HCFramework.API.LINQ;
using Fizzler.Systems.HtmlAgilityPack;
using HtmlAgilityPack;

namespace C
{
    public class LanguageController
    {
        public static List<Language> ListLanguageCache = GetAll();
        /// <summary>
        /// Tra ve Ngon ngu hien tai tuong ung voi ngon ngu viet nam dua vao
        /// </summary>
        /// <param name="contentVN"></param>
        /// <returns></returns>
        public static string GetLang(string contentVN)
        {
            if (GetLanguageCurrent() == GetDefaultLangCode()) return contentVN;

            Language language = Get(contentVN);
            if (language == null)
            {
                language = new Language();
                language.VietNam = contentVN;
                Insert(language);
            }
            switch (LanguageController.GetLanguageCurrent())
            {
                case "VN":
                    return language.VietNam;
                case "EN":
                    return String.IsNullOrEmpty(language.English) ? contentVN : language.English;
            }
            return contentVN;
        }

        /// <summary>
        /// Dich noi dung trong the <t></t> sang ngon ngu hien tai
        /// </summary>
        /// <param name="contentVN"></param>
        /// <returns></returns>
        public static string Translate(string contentVN)
        {
            List<HtmlNode> listLangNode = M.Http.Select(contentVN, "t");
            foreach (HtmlNode langNode in listLangNode)
            {
                string textEN = langNode.InnerText;
                string textTrans = GetLang(textEN);
                contentVN = contentVN.Replace(langNode.OuterHtml, textTrans);
            }
            return contentVN;
        }
        
        public static List<Language> GetAll()
        {
            HCDataContext dc = new HCDataContext();
            return dc.Languages.ToList();
        }
        
        public static string GetLanguageCurrent()
        {
            string ret = M.Common.GetUserData("Lang");
            if (ret == "") ret = GetDefaultLangCode();
            return ret;
        }

        public static string GetDefaultLangCode()
        {
            return "VN";
        }

        public static void SetLanguageCurrent(string langCode)
        {
            M.Common.SetUserData("Lang", langCode);
        }

        public static Language Get(string contentVN)
        {
            return ListLanguageCache.Where(l => l.VietNam == contentVN).FirstOrDefault();
        }

        public static void Insert(Language a)
        {
            HCDataContext db = new HCDataContext();
            db.Languages.InsertOnSubmit(a);
            db.SubmitChanges();
            UpdateCache();
        }

        public static void Update(Language a, HCDataContext db)
        {
            db.SubmitChanges();
            UpdateCache();
        }

        public static void Delete(int id)
        {
            HCDataContext db = new HCDataContext();
            Language a = db.Languages.Where(
                ik => ik.LanguageID == id).First();
            db.Languages.DeleteOnSubmit(a);
            db.SubmitChanges();
            UpdateCache();
        }

        private static void UpdateCache()
        {
            ListLanguageCache = GetAll();
        }

        public static Language GetByID(int id, HCDataContext db)
        {
            return db.Languages.Where(
                ik => ik.LanguageID == id).FirstOrDefault();
        }

        public static Language GetByID(int id)
        {
            return GetByID(id, new HCDataContext());
        }
    }
}